<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="用户手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抽取的红包金额" prop="amountNum">
        <el-input
          v-model="queryParams.amountNum"
          placeholder="请输入抽取的红包金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="红包状态 " prop="isState">
        <el-select
          v-model="queryParams.isState"
          placeholder="发布类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in stateOptions"
            :key="dict.val"
            :label="dict.label"
            :value="dict.val"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['admin:read_package_log:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="read_package_logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日志编号" align="center" prop="id" />
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="用户姓名" align="center" prop="userName" />
      <el-table-column label="用户手机号" align="center" prop="phone" />

      <el-table-column label="抽取的红包金额" align="center" prop="amountNum" />
      <el-table-column label="红包状态" align="center" prop="isState"  :formatter="stateFormatter"/>
      <el-table-column label="该红包截止日期" align="center" prop="endTime" width="180"/>
      <el-table-column label="起始金额" align="center" prop="startNum" />
      <el-table-column label="领取红包类型" align="center" prop="readId" :formatter="readFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" />

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listRead_package_log, getRead_package_log, delRead_package_log, addRead_package_log, updateRead_package_log, exportRead_package_log } from "@/api/admin/read_package_log";
import { listRead_package } from "@/api/admin/read_package";

export default {
  name: "Read_package_log",
  data() {
    return {
      // 遮罩层
      loading: true,
      read_packageList : [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 红包抽取记录表格数据
      read_package_logList: [],
      stateOptions:[
        {
          label:"第一次",
          val : 1,
        },
        {
          label:"进行中",
          val : 2,
        },
        {
          label:"可拆",
          val : 3,
        },
        {
          label:"已经完成",
          val : 4,
        }
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,

        amountNum: null,

        isState: null,

        endTime: null,

        startNum: null,

        readId: null,

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    stateFormatter(row,co){
      var objMap =   this.stateOptions.filter(item => item.val == row.isState);
      if (objMap.length !=0) return objMap[0].label;
    },
    readFormatter(row,co){
      var objMap =   this.read_packageList.filter(item => item.id == row.readId);
      if (objMap.length !=0) return objMap[0].readName;
    },
    /** 查询红包抽取记录列表 */
    getList() {
      this.loading = true;
      listRead_package_log(this.queryParams).then(response => {
        this.read_package_logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      this.getReadList();
    },
    async getReadList ()  {
      var f =   await  listRead_package({pageNum: 1,pageSize: 10000});
      this.read_packageList = f.rows;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        userId: null,

        amountNum: null,

        createTime: null,

        isState: null,

        endTime: null,

        startNum: null,

        readId: null,

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加红包抽取记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRead_package_log(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改红包抽取记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRead_package_log(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRead_package_log(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除红包抽取记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRead_package_log(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有红包抽取记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRead_package_log(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
